package com.ruoyi.qbms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.qbms.domain.QbmsChapter;
import com.ruoyi.qbms.service.IQbmsChapterService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.qbms.domain.QbmsSubject;
import com.ruoyi.qbms.service.IQbmsSubjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题库科目Controller
 * 
 * @author zouhuu
 * @date 2022-10-16
 */
@RestController
@RequestMapping("/qbms/qbmsSubject")
public class QbmsSubjectController extends BaseController
{
    @Autowired
    private IQbmsSubjectService qbmsSubjectService;

    @Autowired
    private IQbmsChapterService qbmsChapterService;

    /**
     * 获取科目树节点
     * @param qbmsSubject
     * @return
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(QbmsSubject qbmsSubject) {
        return AjaxResult.success(qbmsSubjectService.selectQbmsSubjectTreeselect(qbmsSubject));
    }

    /**
     * 查询题库科目列表
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsSubject:list')")
    @GetMapping("/list")
    public TableDataInfo list(QbmsSubject qbmsSubject)
    {
        startPage();
        List<QbmsSubject> list = qbmsSubjectService.selectQbmsSubjectList(qbmsSubject);
        return getDataTable(list);
    }

    /**
     * 导出题库科目列表
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsSubject:export')")
    @Log(title = "题库科目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QbmsSubject qbmsSubject)
    {
        List<QbmsSubject> list = qbmsSubjectService.selectQbmsSubjectList(qbmsSubject);
        ExcelUtil<QbmsSubject> util = new ExcelUtil<QbmsSubject>(QbmsSubject.class);
        util.exportExcel(response, list, "题库科目数据");
    }

    /**
     * 获取题库科目详细信息
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsSubject:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qbmsSubjectService.selectQbmsSubjectById(id));
    }

    /**
     * 新增题库科目
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsSubject:add')")
    @Log(title = "题库科目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QbmsSubject qbmsSubject)
    {
        return toAjax(qbmsSubjectService.insertQbmsSubject(qbmsSubject));
    }

    /**
     * 修改题库科目
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsSubject:edit')")
    @Log(title = "题库科目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QbmsSubject qbmsSubject)
    {
        return toAjax(qbmsSubjectService.updateQbmsSubject(qbmsSubject));
    }

    /**
     * 删除题库科目
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsSubject:remove')")
    @Log(title = "题库科目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            QbmsChapter qbmsChapter = new QbmsChapter();
            qbmsChapter.setSubjectId(id);
            List<QbmsChapter> chapterList = qbmsChapterService.selectQbmsChapterOptions(qbmsChapter);
            if (chapterList.size() > 0) {
                return  AjaxResult.error("该科目下存在章节，不能删除。");
            }
        }
        return toAjax(qbmsSubjectService.deleteQbmsSubjectByIds(ids));
    }
}
